package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Home_Hot_Entity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.NewHttpCofig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.LoginActivity;
import com.shaoshaohuo.app.ui.MainTabActivity;
import com.shaoshaohuo.app.utils.LoginUtils;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/5.
 */

public class HomeListViewAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<Home_Hot_Entity.DataBean.HotListBean> fragment_data;
    private final Path path;
    private final PathMeasure pathMeasure;

    public HomeListViewAdapter(Context context, List<Home_Hot_Entity.DataBean.HotListBean> fragment_data) {
        this.context = context;
        this.fragment_data = fragment_data;
        path = new Path();
        pathMeasure = new PathMeasure();
    }

    public void setdate(List<Home_Hot_Entity.DataBean.HotListBean> fragment_data) {
        this.fragment_data = fragment_data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return fragment_data != null ? fragment_data.size() : 0;
    }

    @Override
    public Home_Hot_Entity.DataBean.HotListBean getItem(int position) {
        return fragment_data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FragmentViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_fragment_item, parent, false);
            holder = new FragmentViewHolder();
            holder.commodity_details = (TextView) convertView.findViewById(R.id.commodity_details);
            holder.commodity_mbn_details = (TextView) convertView.findViewById(R.id.commodity_mbn_details);
            holder.commodity_name = (TextView) convertView.findViewById(R.id.commodity_name);
            holder.commodity_money = (TextView) convertView.findViewById(R.id.commodity_money);
            holder.commodity_shop_money = (TextView) convertView.findViewById(R.id.commodity_shop_money);
            holder.commodity_picture = (ImageView) convertView.findViewById(R.id.commodity_picture);
            holder.commodity_gouwuche = (ImageView) convertView.findViewById(R.id.commodity_gouwuche);
            holder.commodity_level = (TextView) convertView.findViewById(R.id.commodity_level);
            holder.commodity_saled = (TextView) convertView.findViewById(R.id.commodity_saled);
            convertView.setTag(holder);
        } else {
            holder = (FragmentViewHolder) convertView.getTag();
        }
//        SpannableString text_changeBig = new SpannableString("¥" + fragment_data.get(position).getMoney() + "/斤");
//        AbsoluteSizeSpan span = new AbsoluteSizeSpan(20);
//        text_changeBig.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        if (fragment_data.get(position).getMbn_detailsType() == 1) {
            holder.commodity_money.setText("¥" + fragment_data.get(position).getMoney() + "/斤");
        } else if (fragment_data.get(position).getMbn_detailsType() == 2) {
            holder.commodity_money.setText("¥" + fragment_data.get(position).getMoney() + "/个");
        }
//        holder.commodity_shop_money.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        SpannableString add_center_line = new SpannableString("商超价/¥" + fragment_data.get(position).getShop_money());
        add_center_line.setSpan(new StrikethroughSpan(), 0, add_center_line.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.commodity_shop_money.setText(add_center_line);
        holder.commodity_name.setText(fragment_data.get(position).getName());
        holder.commodity_mbn_details.setText(fragment_data.get(position).getMbn_details());
        holder.commodity_details.setText(fragment_data.get(position).getDetails());
        Picasso.with(context).load(fragment_data.get(position).getImg()).placeholder(R.mipmap.zhanzhanzhan).into(holder.commodity_picture);
        holder.commodity_level.setText(fragment_data.get(position).getLevel());
        if (fragment_data.get(position).getBuyNum() != null) {
            holder.commodity_saled.setText("已售:" + fragment_data.get(position).getBuyNum());
        }
//      Log.e("99999999999999", fragment_data.get(position).getMoney());
        holder.commodity_gouwuche.setTag(position);
        holder.commodity_gouwuche.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (LoginUtils.IsLogining()) {
            int position = (int) v.getTag();
            String catid = fragment_data.get(position).getCatid();
            Map<String, String> headerParams = RequestParam.getHeaderParams();
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("catid", catid);
            Okhttputils.Instanse(context).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + NewHttpCofig.App_CarAdd, stringStringHashMap, new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {
                    Log.e("TAG", "error: 请求失败");
                }

                @Override
                public void error(Call call, int code) {

                }

                @Override
                public void suseff(Call call, Response response, Object o) {
                    Log.e("TAG", "error: 请求成功");
                    final MainTabActivity mainTabActivity = (MainTabActivity) context;
                    ((MainTabActivity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mainTabActivity.setnum();
                        }
                    });

                }
            }, BaseEntity.class);
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            int statusBarHeight = BaseActivity.getStatusBarHeight(v.getContext());
            Log.d("TAG", "onClick: statusBarHeight" + statusBarHeight + "---y---" + y);
            path.reset();
            float v1 = (y - statusBarHeight);
            path.moveTo(v.getLeft() + v.getWidth() / 2, v1);
            int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
            int heightPixels = context.getResources().getDisplayMetrics().heightPixels;
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.shouye_weixuan);
            int height = bitmap.getHeight();
            path.quadTo(widthPixels / 2, (heightPixels - v1) / 2 + v1, widthPixels / 2 + bitmap.getWidth(),
                    heightPixels - height / 2);
            pathMeasure.setPath(path, false);
            if (adapterListener != null) {
                adapterListener.pathmove(pathMeasure, v.getLeft() + v.getWidth() / 2, v1);
            }
        } else {
            Intent intent = new Intent();
            intent.setClass(context, LoginActivity.class);
            context.startActivity(intent);
        }
    }

    class FragmentViewHolder {
        TextView commodity_name;
        TextView commodity_mbn_details;
        TextView commodity_details;
        TextView commodity_money;
        TextView commodity_shop_money;
        ImageView commodity_picture;
        ImageView commodity_gouwuche;
        TextView commodity_level;
        TextView commodity_saled;
    }

    private HomeListViewAdapter.AdapterListener adapterListener;

    public void setHomeAdapterListener(HomeListViewAdapter.AdapterListener adapterListener) {
        this.adapterListener = adapterListener;
    }

    public interface AdapterListener {
        void pathmove(PathMeasure pathMeasure, float x, float y);
    }
}
